package org.rea.logic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import com.sun.xml.internal.ws.util.StringUtils;

public abstract class MyWriterbak{



	public MyWriterbak(String fileName) throws IOException {

	}

	public void printBackward(){
		int count = 0;
		MyIterator<String> iter = list.getBackwardIterator();
		do{
			String tempString = iter.nextElement();
			count += countOccurences(tempString, 'a');
			System.out.println(tempString);
		}while(iter.hasMoreElements());	
		System.out.println(count);
	}

	private int countOccurences(String haystack, char needle) {
		int tempCount = 0;
		for (int i=0; i < haystack.length(); i++)
		{
			if (haystack.charAt(i) == needle)
				tempCount++;
		}
		return tempCount;
	}

	public void printBackward(String outName) throws IOException{
		String tempString;
		int count = 0;
		FileWriter fileOut = new FileWriter(outName);
		MyIterator<String> iter = list.getBackwardIterator();

		do{
			tempString = iter.nextElement();
			count += countOccurences(tempString, 'a');
			fileOut.append(tempString);
			fileOut.append('\n');
			fileOut.flush();
		}while(iter.hasMoreElements());
		fileOut.append(count + "");
		fileOut.close();
	}


	public void printForward(){


		MyIterator<String> iter = list.getForwardIterator();
		do{
			System.out.println(iter.nextElement());
		}while(iter.hasMoreElements());	
	}

	public void printForward(String outName) throws IOException{

		String temp;
		FileWriter fileOut = new FileWriter(outName);
		MyIterator<String> iter = list.getForwardIterator();
		do{
			temp = iter.nextElement();
			fileOut.write(temp);
			fileOut.append('\n');
			fileOut.flush();
		}while(iter.hasMoreElements());	
		fileOut.close();
	}




}
